package com.vdf.sisconsolid.persistencemodule.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.vdf.sisconsolid.persistencemodule.converter.ConsolidatorConverter;
import com.vdf.sisconsolid.persistencemodule.dto.ConsolidatorDTO;
import com.vdf.sisconsolid.persistencemodule.dto.KeyValueBean;
import com.vdf.sisconsolid.persistencemodule.model.Consolidator;
import com.vdf.sisconsolid.persistencemodule.model.Person;

public class ConsolidatorDao extends BaseDao {

	private static Logger log = Logger.getLogger(ConsolidatorDao.class);

	public List<KeyValueBean> getConsolidatorList() {
		initTransaction();
		@SuppressWarnings("unchecked")
		List<KeyValueBean> consolidatorList = ConsolidatorConverter
				.toConsolidatorKeyValue(listAllWithoutTransaction("SELECT c FROM Consolidator c WHERE c.endDate IS NUL"));
		commitTransaction();
		return consolidatorList;
	}

	public String getConsolidatorEmail(Long idConsolidator) {
		initTransaction();
		try {
			Consolidator consolidator = getEm().find(Consolidator.class,
					idConsolidator);
			return consolidator.getPerson().getEmail();
		} catch (Throwable e) {
			log.error("Consolidator email was not found");
			e.printStackTrace();
			rollbackTransaction();
		}
		return null;
	}
	
	public List<ConsolidatorDTO> getConsolidatorList(Map<String, Object> parameters) {

		StringBuilder query = new StringBuilder(
				"SELECT consolidator FROM Consolidator consolidator WHERE consolidator.endDate IS NULL ");

		if (parameters.size() > 0) {
			if (parameters.get("family") != null) {
				query.append(" and consolidator.family.id = :family ");
				parameters.put("family",
						((Integer) parameters.get("family")).longValue());
			}
			if (parameters.get("firstName") != null) {
				query.append(" and consolidator.person.firstName = :firstName ");
			}
			if (parameters.get("lastName") != null) {
				query.append(" and consolidator.person.lastName = :lastName ");
			}
			if (parameters.get("document") != null) {
				query.append(" and consolidator.person.document = :document ");
			}
		}
		super.initTransaction();
		List<ConsolidatorDTO> consolidatorDTOList = ConsolidatorConverter.toDTOList((List<Consolidator>) super
				.listAllWithoutTransaction(query.toString(), parameters));
		super.commitTransaction();
		return consolidatorDTOList;
	}
	
	public ConsolidatorDTO find(Long idConsolidator) {
		initTransaction();
		ConsolidatorDTO consolidator = ConsolidatorConverter.toDTO((Consolidator) getEm().find(Consolidator.class,
				idConsolidator));
		commitTransaction();
		return consolidator;
	}
	
	public void cancel(Long idConsolidator) {
		initTransaction();
		try {
			Consolidator consolidator = getEm().find(Consolidator.class, idConsolidator);
			consolidator.setEndDate(new Date());
			getEm().merge(consolidator);
			commitTransaction();
		} catch (Throwable e) {
			e.printStackTrace();
			rollbackTransaction();
		}
	}
	
	public Consolidator merge(ConsolidatorDTO cDTO) {
		Consolidator consolidator = ConsolidatorConverter.toEntity(cDTO);
		consolidator.setStartDate(new Date());
		initTransaction();
		try {
			Person person = getEm().merge(consolidator.getPerson());
			consolidator.setPerson(person);
			consolidator = getEm().merge(consolidator);
			commitTransaction();
		} catch (Throwable e) {
			e.printStackTrace();
			rollbackTransaction();
		}
		return consolidator;
	}
}
